package com.example.anju.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.anju.entity.Information;
import com.example.anju.entity.Result;
import com.example.anju.mapper.InformationMapper;

@Service
public class InformationService {
	@Autowired
	private InformationMapper informationMapper;

	// 创建个人信息
	public Result newInformationById(Information information) {
		Result result = new Result();
		result.setSuccess(false);
		result.setDatas(null);
		try {
			List<Information> informationById = informationMapper
					.findInformationById(information.getInformation_user_id());// 查询是否已经存在
			if (!informationById.isEmpty()) {
				result.setMessage("已经创建了用户信息");
			} else {
				informationMapper.newInformationById(information);
				result.setMessage("创建成功");
				result.setSuccess(true);
				informationById = informationMapper.findInformationById(information.getInformation_user_id());
				result.setDatas(informationById);
			}
		} catch (Exception e) {
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return result;
	}

	// 查看个人信息
	public Result findInformationById(Information information) {
		Result result = new Result();
		result.setSuccess(false);
		result.setDatas(null);
		try {
			List<Information> informationById = informationMapper
					.findInformationById(information.getInformation_user_id());// 查询是否已经存在
			if (informationById.isEmpty()) {
				result.setMessage("未创建用户信息");
			} else {
				result.setMessage("查询成功");
				result.setSuccess(true);
				result.setDatas(informationById);
			}
		} catch (Exception e) {
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return result;
	}

	// 发布/修改个人信息
	public Result postInformationById(Information information) {
		Result result = new Result();
		result.setSuccess(false);
		result.setDatas(null);
		try {
			List<Information> informationById = informationMapper
					.findInformationById(information.getInformation_user_id());
			if (informationById.isEmpty()) {
				informationMapper.newInformationById(information);
				result.setMessage("个人信息完善成功");
				result.setSuccess(true);
				informationById = informationMapper.findInformationById(information.getInformation_user_id());
				result.setDatas(informationById);
			} else {
				informationMapper.postInformationById(information);
				result.setMessage("修改成功");
				result.setSuccess(true);
				informationById = informationMapper.findInformationById(information.getInformation_user_id());
				result.setDatas(informationById);
			}
		} catch (Exception e) {
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return result;
	}
}
